﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Aop;

namespace Flower.Processor
{
    public class ProcessorSettings
    {
        public string ProcessorName { get; set; }
        public int LoadingThreadsCount { get; set; }
        public int BlockingThreadsCount { get; set; }
        public int NonBlockingThreadsCount { get; set; }
        public TimeSpan ProcessUnloadTimeout { get; set; }
        public TimeSpan PendingProcessesCheckingInterval { get; set; }
        public TimeSpan ProcessorTransactionTimeout { get; set; }
        public double MinWaitOnErrorSeconds { get; set; }
        public int MaxRetries { get; set; }
        public TimeSpan NonBlockingActivityTimeout { get; set; }
        public int MaxProcesses { get; set; }
        public IList<IAdvisor> CustomAdvisors { get; set; }

        private List<string> _allowStartStopToUsers = new List<string>();
        private List<string> _allowStartStopToRoles = new List<string>();

        public string AllowStartStopToUsers { set { _allowStartStopToUsers = SplitSemicolonSeparatedList(value); } }
        public string AllowStartStopToRoles { set { _allowStartStopToRoles = SplitSemicolonSeparatedList(value); } }

        internal List<string> AllowStartStopToUsersList { get { return _allowStartStopToUsers; } }
        internal List<string> AllowStartStopToRolesList { get { return _allowStartStopToRoles; } }
        
        private static List<string> SplitSemicolonSeparatedList(string list)
        {
            return list.Split(';').Select(item => item.Trim()).Where(item => item.Length > 0).ToList();
        }

        public ProcessorSettings()
        {
            LoadingThreadsCount = 10;
            NonBlockingThreadsCount = 4;
            BlockingThreadsCount = 10;
            ProcessUnloadTimeout = TimeSpan.FromMinutes(30);
            PendingProcessesCheckingInterval = TimeSpan.FromSeconds(30);
            ProcessorTransactionTimeout = TimeSpan.FromMinutes(1);
            MinWaitOnErrorSeconds = 30;
            MaxRetries = 30;
            NonBlockingActivityTimeout = TimeSpan.FromMinutes(10);
            MaxProcesses = 0;
        }
    }
}
